#include "gpa.h"
#include "ui_gpa.h"
#include<QSqlQueryModel>
#include<QSqlRecord>
#include<QTableView>
#include<QSqlQuery>
#include<QStandardItemModel>//表格相关
#include<QStandardItem>
#include<list>
#include<iterator>
#include<algorithm>

//定义Gpa结构体用于绩点排序
struct Gpa{
    Gpa(QString name,QString id,double gpa):name(name),id(id),gpa(gpa){}
    QString name,id;
    double gpa;
    //重载小于运算符以利用sort排序
    bool operator<(const Gpa&p)const{
        return this->gpa<p.gpa;
    }
};

GPA::GPA(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GPA)
{
    ui->setupUi(this);
}

GPA::~GPA()
{
    delete ui;
}

//返回到管理员主窗口enter_ad
void GPA::on_return_2_clicked()
{
    emit display(4);
}

//修改成绩按钮
//使用QSQLQueryModel只读模式
void GPA::on_pushButton_clicked()
{
    setGPA=new SetGPA(this);
    setGPA->setWindowTitle("修改成绩");
    setGPA->show();

    QSqlQueryModel* model=new QSqlQueryModel;
    model->setQuery("select id,name from st");

    int sum=model->rowCount();//统计数据库中学生总数

    for(int i=0;i<sum;i++){
        QString s="  ";
        QString id_name=model->record(i).value(0).toString()+s+model->record(i).value(1).toString();
        setGPA->ui->comboBox->addItem(id_name);//向comboBox中添加选项

    }


}

//GPA算法函数
double getGPA(int x){
    if(x>=90)return 4;
    else if(x>=85&&x<90)return 3.7;
    else if(x>=82&&x<85)return 3.3;
    else if(x>=78&&x<82)return 3;
    else if(x>=75&&x<78)return 2.7;
    else if(x>=72&&x<75)return 2.3;
    else if(x>=68&&x<72)return 2;
    else if(x>=64&&x<68)return 1.5;
    else if(x>=60&&x<64)return 1;
    else return 0;
}

//成绩排名窗口
void GPA::on_pushButton_3_clicked()
{

    sort_GPA=new sortGPA(this);
    sort_GPA->setWindowTitle("成绩排名");
    sort_GPA->show();

    //添加表头
    //QStandardItemModel常与tableView组成model/view结构
    QStandardItemModel*model=new QStandardItemModel;
    model->setColumnCount(4);
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("排名"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("姓名"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("学号"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("绩点"));

    QSqlQuery query;
    query.exec("select name,st.id,cpp,shudian,lisan,gaoshu,xiandai,dawu\
                     from st inner join gpa\
                     on st.id=gpa.id");

    //GPA计算
    int cpp,shudian,lisan,gaoshu,xiandai,dawu;
    double gpa;
    QString name,id,s_gpa;
    std::list<Gpa> st_list;//Gpa结构体列表



    while(query.next()){

        name=query.value(0).toString();
        id=query.value(1).toString();

        cpp=query.value(2).toInt();
        shudian=query.value(3).toInt();
        lisan=query.value(4).toInt();
        gaoshu=query.value(5).toInt();
        xiandai=query.value(6).toInt();
        dawu=query.value(7).toInt();

        gpa=(getGPA(cpp)*3+getGPA(shudian)*3+getGPA(lisan)*3\
             +getGPA(gaoshu)*5+getGPA(xiandai)*3+getGPA(dawu)*4)/21;
        //s_gpa.number(gpa,'f',2);//gpa格式化，固定保留两位小数

        Gpa g(name,id,-gpa);
        st_list.push_back(g);

    }

    //学生绩点排序并展示
    st_list.sort();
    int i=0;
    for (std::list<Gpa>::iterator iter=st_list.begin();
         iter!=st_list.end();iter++ ) {
        QString rank=QString::number(i+1);//学生排名
        QString s_gpa=QString::number(-iter->gpa);//学生绩点
        model->setItem(i,0,new QStandardItem(rank));
        model->setItem(i,1,new QStandardItem(iter->name));
        model->setItem(i,2,new QStandardItem(iter->id));
        model->setItem(i,3,new QStandardItem(s_gpa));
        i++;
    }


    sort_GPA->ui->tableView->setModel(model);
    //隐藏行号
    sort_GPA->ui->tableView->verticalHeader()->hide();


}



//查看学生成绩按钮
//使用多表内连接查询
void GPA::on_pushButton_2_clicked()
{
    look_GPA=new lookGPA(this);
    look_GPA->setWindowTitle("查看成绩");
    look_GPA->show();

    //只读形式操作
    QSqlQueryModel *model=new QSqlQueryModel;
    //inner join使st表与gpa表连接查询，相等条件为学号
    model->setQuery("select name,st.id,cpp,shudian,lisan,gaoshu,xiandai,dawu\
                     from st inner join gpa\
                     on st.id=gpa.id");


    //设置表格头部标签信息
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("姓名"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("学号"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("c++"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("数字电路"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("离散数学"));
    model->setHeaderData(5,Qt::Horizontal,QObject::tr("高等数学"));
    model->setHeaderData(6,Qt::Horizontal,QObject::tr("线性代数"));
    model->setHeaderData(7,Qt::Horizontal,QObject::tr("大学物理"));

    //借助QTableView显示数据
    look_GPA->ui->tableView->setModel(model);


}
